package com.tom.tran;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 创建生成者对象,指定组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("tran_hello_group");

        // 获取mq连接
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 消费者订阅
        consumer.subscribe("tran", "hello");

        consumer.registerMessageListener((List<MessageExt> msgs, ConsumeConcurrentlyContext context) -> {
            for (MessageExt msg : msgs) {
                String result = new String(msg.getBody(), StandardCharsets.UTF_8);
                System.out.println(result);
            }
            // 标记该消息已经被成功消费
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }
}
